package com.flower.core_service.service.impl;

import com.flower.core_service.object.entity.AuthRole;
import com.flower.core_service.object.entity.AuthRoleMenu;
import com.flower.core_service.mapper.AuthRoleMapper;
import com.flower.core_service.object.request.RoleMenuRequest;
import com.flower.core_service.service.AuthRoleMenuService;
import com.flower.core_service.service.AuthRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author flower
 * @since 2022-03-30
 */
@Service
public class AuthRoleServiceImpl extends ServiceImpl<AuthRoleMapper, AuthRole> implements AuthRoleService {


    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    AuthRoleMenuService authRoleMenuService;

    @Override
    public List<AuthRole> findAllRoles() {
        return baseMapper.findAllRoles();
    }

    @Transactional
    @Override
    public void updateRole(RoleMenuRequest model) {
        AuthRole authRole= model.getRole();
        baseMapper.updateById(authRole);
        List<String> ids= model.getIds();
        //authRoleMenuService.deleteByRoleId(authRole.getId());
        final List<AuthRoleMenu> collect = ids.stream().map(id -> new AuthRoleMenu().setId(id)).collect(Collectors.toList());
        authRoleMenuService.saveBatch(collect);


    }
}
